<?php
namespace App\Libs\Passport;
use \App\Libs\Base\Constant;
use \App\Libs\Exception\RuntimeException;
use \App\Libs\Helper\PasswordHelper;
class ResetPassword extends \App\Libs\Base\Singleton
{
    private $userTable = 't_hera_user';
    public function reset($oldPassword, $newPassword) {
        $user = \Phalcon\DI::getDefault()->get('session')->getUser();
        list($salt, $password) = PasswordHelper::encrypt($oldPassword, $user->salt);

        if ($user->password !== $password) {
            throw RuntimeException::instance(Constant::PASSWORD_ERROR);
        }
        list($salt, $password) = PasswordHelper::encrypt($newPassword);
        $info = array(
            'id' => $user->id,
            'password' => $password,
            'salt' => $salt,
        );
        $sql = 'UPDATE ' . $this->userTable . ' SET password=:password,salt=:salt WHERE id=:id';
        $ret = \App\Libs\Db\HeraPdo::write($sql, $info);
        // for log
        return $ret;
    }
}
